home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue20 / cleantmp2.0 < prev    next >
Encoding:
Text File  |  2002-08-14  |  2.0 KB  |  94 lines

  1. <!--startcut ==========================================================-->
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
  3. <HTML>
  4. <BODY BGCOLOR="#EEE1CC" TEXT="#000000" LINK="#0000FF" VLINK="#0020F0"
  5. ALINK="#FF0000">
  6.  
  7. <H4>
  8. "Linux Gazette...<I>making Linux just a little more fun!</I>"
  9. </H4>
  10.  
  11. <P> <HR> <P> 
  12. <!--===================================================================-->
  13.  
  14. <pre>
  15. #! /usr/bin/perl -w
  16. # cleantmp: Remove old files from /tmp partition
  17. # Copyright (C) 1997 by Guy Geens <Guy.Geens@elis.rug.ac.be>
  18. # Snail Mail:
  19. # Zwijnaardsesteenweg 183
  20. # 9000 Gent
  21. # Belgium
  22.  
  23. use File::Find;
  24.  
  25. $tmpdir = '/tmp/';
  26. chdir ($tmpdir) || die "$tmpdir not accessible: $!";
  27. if ($> == 0) {            # Is euid == 0?
  28.     $test = 0;
  29. } else {
  30. # Not run by root - test only
  31.     $test = 1;
  32. }
  33.  
  34. @list = ();
  35.  
  36. &find(\&do_files, $tmpdir);
  37. &find(\&do_dirs, $tmpdir);
  38.  
  39. if (@list) {
  40.     print "Cleaned $tmpdir\n";
  41.     print "Deleted files are:\n";
  42.     for (sort @list) {
  43.     print "$_\n";
  44.     }
  45. }
  46.  
  47. exit;
  48.  
  49. sub do_files {
  50.     (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
  51.     (-f _ || -l _ ) &&
  52.         (int(-C _) > 3) &&
  53.         ! ((/^\.X.*lock$/ || /^quota\.user$/ || /^quota.group$/)
  54.            && $uid == 0) &&
  55.                &removefile ($_) && push @list, $File::Find::name;
  56. }
  57.  
  58. sub do_dirs {
  59.     (/^\..*-unix$/ && $uid ==0) && ($File::Find::prune = 1) ||
  60.     (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
  61.         -d _ && ($nlink == 2) &&
  62.         ! (/^lost\+found$/ && $uid == 0) &&
  63.             &removedir ($_) && push @list, "$File::Find::name/";
  64. }
  65.  
  66. sub removedir {
  67.     if ( $test ) {
  68.     1;
  69.     } else {
  70. # Can't use @_: rmdir doesn't take a list argument
  71.     rmdir $_[0];
  72.     }
  73. }
  74.  
  75. sub removefile {
  76.     if ( $test ) {
  77.     1;
  78.     } else {
  79.     unlink @_;
  80.     }
  81. }
  82. </pre>
  83.  
  84. <!--===================================================================-->
  85. <P> <hr> <P> 
  86. <center><H5>Copyright © 1997, Guy Geens<BR> 
  87. Published in Issue 20 of the Linux Gazette, August 1997</H5></center>
  88.  
  89. <!--startcut ==========================================================-->
  90. </BODY>
  91. </HTML>
  92. <!--endcut ============================================================-->
  93.  
  94.